package cn.supermaster.management.common.utils;

import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.common.utils
 * @ClassName: ThreadUtils
 * @Description: [线程相关工具类]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/6 21:08
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
public class ThreadUtils {
    private static final Logger logger = LoggerFactory.getLogger(ThreadUtils.class);

    /**
     * @Description: [sleep等待, 单位为毫秒]
     * <p></P>
     * @Title: sleep
     * @Author: Lance Ting
     * @Date: 2021-01-06
     * @Param: milliseconds
     * @Return: void
     */
    public static void sleep(long milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e) {
            return;
        }
    }

    /**
     * @Description: [停止线程池]
     * <p>
     * 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务.
     * 如果超时, 则调用shutdownNow, 取消在workQueue中Pending的任务,并中断所有阻塞函数.
     * 如果仍超時，則強制退出.
     * 另对在shutdown时线程本身被调用中断做了处理.
     * </P>
     * @Title: shutdownAndAwaitTermination
     * @Author: Lance Ting
     * @Date: 2021-01-06
     * @Param: pool
     * @Return: void
     */
    public static void shutdownAndAwaitTermination(ExecutorService pool) {
        if (pool != null && !pool.isShutdown()) {
            pool.shutdown();
            try {
                if (pool.awaitTermination(120, TimeUnit.SECONDS)) return;
                //
                pool.shutdownNow();
                if (!pool.awaitTermination(120, TimeUnit.SECONDS)) {
                    logger.info("Pool did not terminate");
                }
            } catch (InterruptedException ie) {
                pool.shutdownNow();
                Thread.currentThread().interrupt();
            }
        }
    }

    /**
     * @Description: [打印线程异常信息]
     * <p></P>
     * @Title: printException
     * @Author: Lance Ting
     * @Date: 2021-01-6
     * @Param: runnable
     * @Param: throwable
     * @Return: void
     */
    public static void printException(Runnable runnable, Throwable throwable) {
        if (throwable == null && runnable instanceof Future<?>) {
            try {
                Future<?> future = (Future<?>) runnable;
                if (future.isDone()) {
                    future.get();
                }
            } catch (CancellationException e) {
                throwable = e;
            } catch (ExecutionException e) {
                throwable = e.getCause();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        if (throwable != null) {
            logger.error(throwable.getMessage(), throwable);
        }
    }
}
